package com.xm.settlement.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.mapper.AppShopReturnMapper;
import com.xm.settlement.model.AppExchangeLog;
import com.xm.settlement.model.AppOrderManage;
import com.xm.settlement.model.AppShopReturn;
import com.xm.settlement.model.AppUserManage;
import com.xm.settlement.model.vo.ShopReturnVo;
import com.xm.settlement.service.AppExchangeLogService;
import com.xm.settlement.service.AppUserManageService;
import com.xm.settlement.service.IAppOrderManageService;
import com.xm.settlement.service.IAppShopReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/3/13.
 */
@Service
public class AppShopReturnServiceImpl extends SuperServiceImpl<AppShopReturnMapper,AppShopReturn>  implements IAppShopReturnService {
    @Autowired
    private AppShopReturnMapper appShopReturnMapper;
    @Autowired
    private IAppOrderManageService appOrderManageService;
    @Autowired
    private AppUserManageService appUserManageService;
    @Autowired
    private AppExchangeLogService appExchangeLogService;

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        Page<AppShopReturn> page = new Page<AppShopReturn>(pageInfo.getNowpage(), pageInfo.getSize());
        List<AppShopReturn> list = appShopReturnMapper.selectDataGridPage(page);
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
    }

    @Override
    public ShopReturnVo selectShopReturnVo(Long id) {
        return appShopReturnMapper.selectShopReturnVo(id);
    }

    /**
     * 退货审核操作
     * * */
    @Override
    public void addAuditReturn(AppShopReturn appShopReturn) {
        boolean b = appShopReturn.getStatus() == 1 ? true:false;
        AppOrderManage appOrderManage = new AppOrderManage();
        appOrderManage.setOrderCode(appShopReturn.getOrderNo());
        if(b){
            //////////////////////////////
            appOrderManage = appOrderManageService.selectOne(appOrderManage);

            AppShopReturn appShopReturn1 = appShopReturnMapper.selectById(appShopReturn.getId());
            appShopReturn1.setStatus(appShopReturn.getStatus());
            appShopReturn1.setUpdateId(appShopReturn.getUpdateId());
            appShopReturn1.setUpdateTime(new Date());
            appShopReturnMapper.updateById(appShopReturn1);
            //////////////////////////////////
            appOrderManage.setOrderStatus(21);
            appOrderManage.setUpdateId(appShopReturn.getUpdateId());
            appOrderManage.setUpdateTime(appShopReturn.getUpdateTime());
            appOrderManageService.updateById(appOrderManage);

            //////////////////////////////////////////////////////////////////
            AppUserManage appUserManage = new AppUserManage();
            appUserManage.setId(appOrderManage.getCreateId());
            appUserManage = appUserManageService.selectOne(appUserManage);
            appUserManage.setCash_score(appOrderManage.getGoodsTprice() +
                    (appUserManage.getCash_score()==null? 0 : appUserManage.getCash_score() ));
            appUserManageService.updateById(appUserManage);

            AppExchangeLog appExchangeLog = new AppExchangeLog();
            appExchangeLog.setCreateTime(new Date());
            appExchangeLog.setCreateId(appShopReturn.getUpdateId());
            appExchangeLog.setExchangeCode("E"+ System.currentTimeMillis());
            appExchangeLog.setOrderCode(appOrderManage.getOrderCode());
            appExchangeLog.setType(1);
            appExchangeLog.setId(appShopReturn.getUpdateId());
            appExchangeLogService.insert(appExchangeLog);
        }else{
            //2.修改状态
            AppShopReturn appShopReturn1 = appShopReturnMapper.selectById(appShopReturn.getId());
            appShopReturn1.setStatus(appShopReturn.getStatus());
            appShopReturn1.setUpdateId(appShopReturn.getUpdateId());
            appShopReturn1.setUpdateTime(new Date());
            appShopReturnMapper.updateById(appShopReturn1);
        }
    }


}
